package com.atguigu.gulimall.product.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.SkuImagesEntity;
import com.atguigu.gulimall.product.entity.SkuInfoEntity;
import com.atguigu.gulimall.product.entity.SpuInfoDescEntity;
import com.atguigu.gulimall.product.entity.vo.SeckillInfoVo;
import com.atguigu.gulimall.product.entity.vo.SkuItemSaleAttrVo;
import com.atguigu.gulimall.product.entity.vo.SkuItemVo;
import com.atguigu.gulimall.product.entity.vo.SpuItemBaseAttrVo;
import com.atguigu.gulimall.product.feign.SeckillFeignService;
import com.atguigu.gulimall.product.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class SkuItemServiceImpl implements SkuItemService {

    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    SkuImagesService skuImagesService;

    @Autowired
    SpuInfoDescService spuInfoDescService;

    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    SeckillFeignService seckillFeignService;

    @Autowired
    ThreadPoolExecutor executor;

    @Override
    public SkuItemVo skuItem(Long skuId) throws ExecutionException, InterruptedException {

        //任务3 4 5 依赖任务1 ，需要等1结束后启动
        //任务2和任务一没关系，各自起各自的
        //等所有任务完成返回数据
        SkuItemVo skuItemVo = new SkuItemVo();

        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfoEntity = skuInfoService.getById(skuId);
            skuItemVo.setInfo(skuInfoEntity);
            return skuInfoEntity;
        },executor);

        CompletableFuture<Void> saleFuture = infoFuture.thenAcceptAsync((res) -> {
            //3。获取当前sku所属的spu的所有sku销售属性组合
            List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrBySpuId(res.getSpuId());
            skuItemVo.setSaleAttrVos(saleAttrVos);
        }, executor);

        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res) -> {
            //4。获取spu的介绍
            SpuInfoDescEntity descEntity = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setDesc(descEntity);
        }, executor);

        CompletableFuture<Void> groupFuture = infoFuture.thenAcceptAsync((res) -> {
            //5。获取spu的规格参数信息
            List<SpuItemBaseAttrVo> attrVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setBaseAttrVos(attrVos);
        }, executor);

        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            //2。获取sku的图片信息 pms_sku_images
            List<SkuImagesEntity> imagesEntitys = skuImagesService.selectImagesBySkuId(skuId);
            skuItemVo.setImages(imagesEntitys);
        }, executor);

        CompletableFuture<Void> seckillFuture = CompletableFuture.runAsync(() -> {
            //3.查询当前sku是否参与秒杀优惠
            R skuSeckillInfo = seckillFeignService.getSkuSeckillInfo(skuId);
            if (skuSeckillInfo.getCode() == 0){
                SeckillInfoVo data = skuSeckillInfo.getData(new TypeReference<SeckillInfoVo>() {
                });
                skuItemVo.setSeckillInfoVo(data);
            }
        }, executor);

        //用get阻塞等待所有结果借宿
        CompletableFuture.allOf(saleFuture,descFuture,groupFuture,imageFuture,seckillFuture).get();

//        //1。获取sku的基本信息 pms_sku_info
//        SkuInfoEntity skuInfoEntity = skuInfoService.getById(skuId);
//        skuItemVo.setInfo(skuInfoEntity);
//
//        Long catalogId = skuInfoEntity.getCatalogId();
//        Long spuId = skuInfoEntity.getSpuId();
//
//        //2。获取sku的图片信息 pms_sku_images
//        List<SkuImagesEntity> imagesEntitys = skuImagesService.selectImagesBySkuId(skuId);
//        skuItemVo.setImages(imagesEntitys);
//
//        //3。获取当前sku所属的spu的所有sku销售属性组合
//        List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrBySpuId(spuId);
//        skuItemVo.setSaleAttrVos(saleAttrVos);
//
//        //4。获取spu的介绍
//        SpuInfoDescEntity descEntity = spuInfoDescService.getById(spuId);
//        skuItemVo.setDesc(descEntity);
//
//        //5。获取spu的规格参数信息
//        List<SpuItemBaseAttrVo> attrVos = attrGroupService.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
//        skuItemVo.setBaseAttrVos(attrVos);

        return skuItemVo;
    }
}
